-
-
Notifications
You must be signed in to change notification settings - Fork 3.4k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
useTrackedState from reactive-react-redux #1503
Conversation
Deploy preview for react-redux-docs ready! Built with commit 7d20db4 |
Cool. Primary points of concern:
|
I made a simple counter + textbox app and built with CRA. useTrackedState + useSelector
useSelector only
useTrackedState only
|
I'm not super good at documentation. Would appreciate for any help (not only for contents but also for grammar.) |
cache: new WeakMap() | ||
} | ||
}) | ||
useIsomorphicLayoutEffect(() => { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
While it's not an immediate concern, I'm noticing that we now have 3 different parts of our codebase where we're subscribing to the store in a hook. That may be worth looking at to see if there's some way to abstract it.
Co-Authored-By: Mark Erikson <[email protected]>
I think I did it so, but it's not showing in the preview. Is anything wrong? |
Love this! My only concern is there seems to be too many caveats, and holes in which to fall through if not fully aware of how it works. |
I understand your concern. There are some ideas to mitigate caveats, but they are not free either. My question would be how big those holes are in real world apps. |
For me, I think it would be all worth it, but any time there is a hole, it is inevitable that someone will fall in, and not take that fall well at all 😕 @dai-shi how have users of reactive-react-redux handled this? Have you experienced many reports or complaints about the caveats? |
There are not many users of reactive-react-redux, so I can't tell. That's the point of having this in react-redux. |
Yeah, think that would be a good idea. The first caveat should be easy to track and to throw an error/warning. Not sure about the others, but if not possible to track and warn, then maybe create a few eslint rules? |
The first one is actually difficult if you consider custom hooks. can eslint rules detect it? For the second one, I have an idea with heuristics, only effective in DEV. I'll tackle this first. For the third one, we have several options: a) allow leaking proxies and deproxify before wrapping with a new proxy, b) disallow it and detect it in DEV before wrapping with a new proxy, c) disallow it and detect it in DEV with a patched |
I was too naive. My first trial reveals that it comes with false positives quite easily. I don't think we can do much at the hooks level. One thing it would be helpful is to show "what path is tracked" in console. But, this means developers have to understand what is "tracking by proxies". We have to decide one important point, whether this hook is for beginners or not. I have assumed this hook is for beginners from the start. If this is the case, I have a crazy idea. We can make the hook so that it will never forget the tracked path by default. That will erase the second caveat. In such a case, I could add an functionality to opt-out the default behavior (meaning, to clear the tracked path in each render) for optimization, or just encourage the use of Feedback welcome, including request for clarification. On second thought, I think what everyone would expect for react-redux is performance and predictability. In such a case, I don't take the crazy idea, and seek/improve debugability of "tracking". |
Let me summarize my thoughts about caveats with possible ideas to mitigate them. Caveat 1: Proxied states are referentially equal only in per-hook basisUnless we use custom hooks, it's not very likely to be misused (and we could detect it by eslint rules.) Caveat 2: An object referential change doesn't trigger re-render if an property of the object is accessed in previous renderThis is a pitfall that's hard to find if one falls into. Caveat 3: Proxied state shouldn't be used outside of renderAs I said, there are several paths. For this one, I need more feedback.
|
I've made a few improvements.
|
It turns out that this feature can be built on top of useSelector. |
@markerikson @dai-shi hi!) I'd like to know, why this pull request was not merged?) Looks like useful feature. Di you need any help with it? |
@artem-malko react-tracked offers the same capability opt-in. If you are interested in it, check the (naive) example: https://react-tracked.js.org/docs/tutorial-redux-01 Also, check out the proxy-memoize in the redux docs for similar (not the same) capability as selectors. What we can do (help) is to show some use cases, how powerful these approaches are, and possible limitations. |
Given that we're working on v8 right now and it's all been migrated to TS, and that I'm still open to the idea of possibly adding @dai-shi , thoughts? |
Yeah, I guess I close this PR with two alternatives.
|
Thx for the info) |
For #1502.
This brings useTrackedState from reactive-react-redux and adjusts for react-redux store context.
TODOs: